package com.api.tournament.service.impl;

import com.api.tournament.contonst.Const;
import com.api.tournament.dao.StageMapper;
import com.api.tournament.dao.StageRankMapper;
import com.api.tournament.dao.TournamentMapper;
import com.api.tournament.dao.TournamentTeamMapper;
import com.api.tournament.model.Stage;
import com.api.tournament.model.StageRank;
import com.api.tournament.model.TournamentTeam;
import com.api.tournament.service.TournamentTeamService;
import com.api.core.service.AbstractService;
import com.api.tournament.vo.FreeRankVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/03/31.
 */
@Service
@Transactional
public class TournamentTeamServiceImpl extends AbstractService<TournamentTeam> implements TournamentTeamService {
    @Resource
    private TournamentTeamMapper tournamentTeamMapper;
    @Resource
    private StageRankMapper stageRankMapper;
    @Resource
    private TournamentMapper tournamentMapper;
    @Resource
    private StageMapper stageMapper;

    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = tournamentTeamMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result freeRank(List<FreeRankVo> freeRankVos) {
        for (int i = 0; i < freeRankVos.size(); i++) {
            TournamentTeam tournamentTeam =   tournamentTeamMapper.selectByPrimaryKey(freeRankVos.get(i).getTournamentTeamId());
            tournamentTeam.setIntegral(freeRankVos.get(i).getIntegral());
            tournamentTeam.setRank(freeRankVos.get(i).getRank());
            List<Stage> stages = stageMapper.selectByTournamentId(tournamentTeam.getTournamentId());
            if(stages==null || stages.size()<1 || !stages.get(0).getFormat().equals(Const.GAME_FORMAT_FREE)){
                return  ResultGenerator.genFailResult(400,"This stage is not free for all format");
            }
            update(tournamentTeam);
            StageRank stageRank = stageRankMapper.findByStageIdAndUserId(stages.get(0).getId(),tournamentTeam.getTournamentUserId());
            stageRank.setRank(tournamentTeam.getRank());
            stageRank.setScore(tournamentTeam.getIntegral());
            stageRank.setImages(freeRankVos.get(i).getImages());
            stageRankMapper.updateByPrimaryKey(stageRank);
        }
        return ResultGenerator.genSuccessResult();
    }
}
